package com.neusoft.dibuy.service.impl;

import com.neusoft.dibuy.properties.IpCounterProperties;
import com.neusoft.dibuy.service.IpCounterService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

public class IpCounterServiceImpl implements IpCounterService {
    @Autowired
    private HttpServletRequest request;

    @Autowired
    private IpCounterProperties ipCounterProperties;

    private static Map<String ,Integer> map =new HashMap<>();

    public void count()
    {
        String ip =  request.getRemoteAddr();
        Integer i = Objects.isNull(map.get(ip))?0:map.get(ip);
        i++;
        map.put(ip,i);
        map.forEach((k,v)->{
            System.out.println( "IP : " +  k + ",  Times :" +  v );
            if (v > ipCounterProperties.getBalance()){
                System.out.println("IP : " +  k + "  should be  blocked");
            }
        });

    }
}
